#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
FileSearchTool 快速测试脚本
简化版测试，用于快速验证功能
"""

from tools.FileSearchTools import FileSearchTool
import time


def quick_test():
    """快速测试FileSearchTool基本功能"""
    print("🚀 FileSearchTool 快速测试")
    print("=" * 40)
    
    try:
        # 1. 初始化搜索工具
        print("1. 初始化搜索工具...")
        search_tool = FileSearchTool(
            collection_name="massive_file_search"
        )
        print("   ✅ 初始化成功")
        
        # 2. 检查集合状态
        stats = search_tool.get_collection_stats()
        print(f"2. 当前集合状态:")
        print(f"   集合名称: {stats['collection_name']}")
        print(f"   实体数量: {stats['total_entities']}")
        
        # 3. 如果集合为空，提示用户
        if stats['total_entities'] == 0:
            print("\n⚠️ 集合为空，请先运行 fileSearchDemo.py 建立索引")
            return
        
        # 4. 执行测试搜索
        test_queries = [
            "找找所有的Python文件",
            "有什么配置相关的文件",
            "配置",
            "图片"
        ]
        
        print(f"\n3. 执行搜索测试...")
        for query in test_queries:
            print(f"\n   搜索: '{query}'")
            
            start_time = time.time()
            results = search_tool.search_files(query, top_k=3)
            search_time = time.time() - start_time
            
            print(f"   用时: {search_time:.4f}秒")
            print(f"   结果: {len(results)}个")
            
            for i, result in enumerate(results, 1):
                print(f"   {i}. {result['file_name']} (相似度: {result['similarity_score']:.3f})")
        
        print(f"\n✅ 快速测试完成!")
        
    except Exception as e:
        print(f"❌ 测试失败: {str(e)}")
        print("请检查:")
        print("1. Milvus服务是否正常运行")
        print("2. 网络连接是否正常")
        print("3. 认证信息是否正确")


def connection_test():
    """仅测试Milvus连接"""
    print("🔌 Milvus连接测试")
    print("=" * 30)
    
    try:
        search_tool = FileSearchTool(collection_name="connection_test")
        stats = search_tool.get_collection_stats()
        
        print("✅ 连接成功!")
        print(f"   服务器: c-f88be548ab3aa84d.milvus.aliyuncs.com:19530")
        print(f"   集合: {stats['collection_name']}")
        print(f"   状态: {'已加载' if stats['is_loaded'] else '未加载'}")
        
    except Exception as e:
        print(f"❌ 连接失败: {str(e)}")


if __name__ == "__main__":
    print("FileSearchTool 测试选项:")
    print("1. 快速功能测试")
    print("2. 仅测试连接")
    
    choice = input("请选择 (1-2): ").strip()
    
    if choice == "1":
        quick_test()
    elif choice == "2":
        connection_test()
    else:
        print("无效选择，运行快速测试...")
        quick_test()